package com.nintendo.rabbitmq;

import com.nintendo.pojo.netty.ChatMsg;
import com.nintendo.service.ChatMessageService;
import com.nintendo.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Auther 风间影月
 */
@Component
@Slf4j
public class RabbitMQConsumer {

    @Resource
    private ChatMessageService chatMessageService;

    @RabbitListener(queues = "test_queue")
    public void watchQueue(String payload, Message message) {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        log.info("routingKey = " + routingKey);
        if (routingKey.equals("imooc.wechat.wechat.msg.send")) {
            String msg = payload;
            ChatMsg chatMsg = JsonUtils.jsonToPojo(msg, ChatMsg.class);
            if(chatMsg.getReceiverId()!=null){
                chatMessageService.saveMsg(chatMsg);
            }
        }

    }

}
